<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="header-left">
            <img class="logo" src="../assets/logo_1.png" alt="logo" />
            <div class="web-title">智界峨眉</div>
            <div class="collapse-btn" @click="collapseChage">
                <el-icon v-if="sidebar.collapse">
                    <Expand />
                </el-icon>
                <el-icon v-else>
                    <Fold />
                </el-icon>
            </div>
        </div>
        <div class="header-right">
            <div class="header-user-con">
                <el-icon style="margin-right: 15px; margin-top: 3px; color: rgb(130, 135, 140); font-size: 18px; cursor: pointer;" @click="refreshPage"><Refresh /></el-icon>
                <!-- 消息提示按钮 -->
                <el-tooltip content="欢迎登录" placement="top">
                    <el-icon style="margin-right: 5px; margin-top: 2px; cursor: pointer;">
                        <ChatDotSquare />
                    </el-icon>
                </el-tooltip>
                <!-- 全屏按钮 -->
                <el-button style="border: 0; background: ffffff">
                    <el-icon @click="openWindows">
                        <FullScreen />
                    </el-icon>
                </el-button>
                <!-- 用户头像 -->
                <el-avatar class="user-avator" :size="30" :src="imgurl" />
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{ username }}
                        <el-icon class="el-icon--right">
                            <arrow-down />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="individual">个人中心</el-dropdown-item>
                            <el-dropdown-item command="setting">公告</el-dropdown-item>
                            <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useSidebarStore } from '../store/aside'; // 导入侧边栏状态管理
import { useRouter } from 'vue-router';
import { GetAvatar } from '../api/work.js'; // 假设此 API 返回用户头像和用户名

// 定义头像和用户名
const imgurl = ref('');
const username = ref('');

// 从 API 获取头像和用户名
const Avatar = async (id: number) => {
    const result = await GetAvatar(id);
    if (result.data.code === 200) {
        const data = result.data.data;
        imgurl.value = data.avatar;
        username.value = data.username;
    }
}

// 使用状态管理侧边栏
const sidebar = useSidebarStore();
// 侧边栏折叠功能
const collapseChage = () => {
    sidebar.handleCollapse();
};

// 在组件挂载时执行逻辑
onMounted(() => {
    if (document.body.clientWidth < 1500) {
        collapseChage();
    }
    Avatar(1); // 假设 ID 为 1，获取用户数据
});

// 路由功能
const router = useRouter();
// 处理用户操作命令
const handleCommand = (command: string) => {
    if (command === 'loginout') {
        localStorage.removeItem('vuems_name'); // 清除登录状态
        router.push('/login'); // 跳转到登录页
    } else if (command === 'individual') {
        router.push('/individual'); // 跳转到个人中心
    } else if (command === 'setting') {
        router.push('/setting'); // 跳转到公告页
    }
};

// 实现网页全屏功能
const openWindows = () => {
    const elem = document.documentElement;
    if (elem.requestFullscreen) {
        elem.requestFullscreen();
    } else if (elem.mozRequestFullScreen) { // Firefox
        elem.mozRequestFullScreen();
    } else if (elem.webkitRequestFullscreen) { // Chrome, Safari and Opera
        elem.webkitRequestFullscreen();
    } else if (elem.msRequestFullscreen) { // IE/Edge
        elem.msRequestFullscreen();
    }
};

const refreshPage = () => {
  window.location.reload(); // 刷新页面
};
</script>

<style scoped>
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    color: var(--header-text-color);
    background-color: var(--header-bg-color);
    border-bottom: 2px solid black;
    background: #ffffff;
}

.header-left {
    display: flex;
    align-items: center;
    padding-left: 20px;
    height: 100%;
}

.logo {
    width: 35px;
}

.web-title {
    margin: 0 40px 0 10px;
    font-size: 22px;
}

.header-right {
    float: right;
    padding-right: 70px;
}

.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}

.collapse-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    padding: 0 10px;
    cursor: pointer;
    opacity: 0.8;
    font-size: 22px;
}

.collapse-btn:hover {
    opacity: 1;
}

.user-avator {
    margin: 0 10px 0 20px;
}

.el-dropdown-link {
    color: var(--header-text-color);
    cursor: pointer;
    display: flex;
    align-items: center;
}

.el-dropdown-menu__item {
    text-align: center;
}

.el-popper .is-customized {
    padding: 6px 12px;
    background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper .is-customized .el-popper__arrow::before {
    background: linear-gradient(45deg, #b2e68d, #bce689);
    right: 0;
}
</style>
